package com.okkiovispo.addmachine;




import java.util.ArrayList;



import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Vibrator;

public class MainActivity extends ListActivity {

	TextView display;
	Button add;
	double somma;
	double input;
	EditText editText;
	Vibrator vibrator;
	
	
	/** Button for keyboard */
	Button button0,button1,button2,button3,
    button4,button5,button6,button7,button8,button9,buttonPlus,buttonMinus,buttonMultiply,
    buttonDivide,buttonEqual,buttonPoint,buttonDel,buttonReset,
    button_sin,button_cos,button_tan,button_squared_2,button_root,button_back,button_dec,
    button_bin,button_pi;
	char press;
	
	
	String sum="", one,oneAgain="",two,twoAgain="",three,threeAgain="",four,fourAgain="",five,fiveAgain="",
            six,sixAgain,seven,sevenAgain="",eight,eightAgain="",nine,nineAgain="",
            zero;
	
    /** Items entered by the user is stored in this ArrayList variable */
    ArrayList<String> list = new ArrayList<String>();

    /** Declaring an ArrayAdapter to set items to ListView */
    ArrayAdapter<String> adapter;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

	    super.onCreate(savedInstanceState);
	    /** Setting a custom layout for the list activity */
	    setContentView(R.layout.main);
	    vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
	    somma = 0;
	    /** Reference to the pad button of the layout activity_main.xml */
	    button0=(Button)findViewById(R.id.button0);
        button1=(Button)findViewById(R.id.button1);
        button2=(Button)findViewById(R.id.button2);
        button3=(Button)findViewById(R.id.button3);
        button4=(Button)findViewById(R.id.button4);
        
        button5=(Button)findViewById(R.id.button5);
        button6=(Button)findViewById(R.id.button6);
        button7=(Button)findViewById(R.id.button7);
        button8=(Button)findViewById(R.id.button8);
        button9=(Button)findViewById(R.id.button9);
        buttonPoint=(Button)findViewById(R.id.buttonPoint);
        button_back=(Button)findViewById(R.id.back);
        
        editText = (EditText) findViewById(R.id.txtItem);
	    /** Reference to the add button of the layout activity_main.xml */
	    add = (Button) findViewById(R.id.btnAdd);
	    
	    /** Reference to the delete button of the layout activity_main.xml */
	    Button btnDel = (Button) findViewById(R.id.btnDel);
	    /** Reference to the reset button of the layout activity_main.xml */
	    Button btnReset = (Button) findViewById(R.id.reset);
	    
	    display = (TextView) findViewById(R.id.textView1);
	    /** Defining the ArrayAdapter to set items to ListView */
	    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, list);
	
	    /** Defining a click event listener for the button "Add" */
	    OnClickListener listener = new OnClickListener() {
	                    @Override
	                    public void onClick(View v) {
	                    	
	                            EditText edit = (EditText) findViewById(R.id.txtItem);
	                            input = 0;
	                            if (edit.getText().length() != 0) {
	                    			press = '=';
		                            input = Double.parseDouble(edit.getText().toString());
		                            somma = somma + input;
				            		display.setText("Total: " + (String.format("%.2f", somma)));
		                            list.add(String.format("%.2f", input));
		                            edit.setText("");
		                            adapter.notifyDataSetChanged();
	                    		}
			            		
	                    }
	            };
	            
	    /** Defining a click event listener for the button "Delete" */
	    OnClickListener listenerDel = new OnClickListener() {
	    	@Override
	    	public void onClick(View v) {
	    		/** Getting the checked items from the listview */
	    		SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
	    		int itemCount = getListView().getCount();
	    		
	    		for(int i=itemCount-1; i >= 0; i--){
	    			if(checkedItemPositions.get(i)){	 
	    				if (list.get(i).length() != 0) {
                            input = Double.parseDouble(list.get(i).toString());
                            somma = somma - input;
                            display.setText("Total: " + (String.format("%.2f", somma)));
                    		}
	    				adapter.remove(list.get(i));
	    			}
	    		}	
	    		checkedItemPositions.clear();
	    	    adapter.notifyDataSetChanged();
	    	    
	    	   
	    	    
	    	}
	    };            
	
	    /** Setting the event listener for the add button */
	    add.setOnClickListener(listener);
	    
	    /** Setting the event listener for the delete button */
	    btnDel.setOnClickListener(listenerDel);    
	
	    /** Setting the adapter to the ListView */
	    setListAdapter(adapter);
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_menu, menu);
		menu.add(0, Menu.FIRST, 5, R.string.reset);
		menu.add(0, Menu.FIRST+1, 5, R.string.exit);
		menu.add(0, Menu.FIRST+2, 5, R.string.action_settings);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
        case Menu.FIRST:
        	reset(display);
        	return true;
        case Menu.FIRST+1:
        	finish();
        return true;
        case Menu.FIRST+2:
        	settings(display);
        return true;
       
        }
		return super.onOptionsItemSelected(item);
	}

	public void reset(View view) {


		 AlertDialog.Builder builder = new AlertDialog.Builder(this);

		 builder.setTitle("Reset");
		 builder.setMessage("do you want confirm this action?");
		 editText = (EditText) findViewById(R.id.txtItem);
		 builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {

			 public void onClick(DialogInterface dialog, int which) {
				 // Do do my action here
				 vibrator.vibrate(60);
				 somma = 0;
				 display.setText("Total: " + (String.format("%.2f", somma)));
				 editText.setText("");

				list.removeAll(list);
 		
				 adapter.notifyDataSetChanged();
 	    
				 dialog.dismiss();
			 }

		 });

		 builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {

			 @Override
			 public void onClick(DialogInterface dialog, int which) {
				 // I do not need any action here you might
				 dialog.dismiss();
			 }
		 });

		 AlertDialog alert = builder.create();
		 alert.show();
		 Toast.makeText(this, "reset", Toast.LENGTH_LONG).show();  
		
	 }
	
	public void settings(View view) {


		 AlertDialog.Builder builder = new AlertDialog.Builder(this);

		 builder.setTitle("settings");
		 builder.setMessage("versione beta, opzioni non disponibili");
		 editText = (EditText) findViewById(R.id.txtItem);
		 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

			 public void onClick(DialogInterface dialog, int which) {
				 // Do do my action here
				 vibrator.vibrate(600);
//				 sum = 0;
//				 display.setText("Il totale e' " + (String.format("%.2f", sum)));
//				 editText.setText("");
//
//				list.removeAll(list);
//		
//				 adapter.notifyDataSetChanged();
//	    
				 dialog.dismiss();
			 }

		 });

//		 builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
//
//			 @Override
//			 public void onClick(DialogInterface dialog, int which) {
//				 // I do not need any action here you might
//				 dialog.dismiss();
//			 }
//		 });

		 AlertDialog alert = builder.create();
		 alert.show();
		 Toast.makeText(this, "reset", Toast.LENGTH_LONG).show();  
		
	 }
	/** Listener for pad Button */
	  public void onClickListener0(View v)
	    {
	        vibrator.vibrate(30);
	        if(press=='=')
	        {
	            onClickListenerReset(buttonReset);
	        }
	        
	        if(sum !="")
	        {
	            zero=(String)button0.getText();
	            
	            sum=sum+zero;
	            editText.setText(sum);
	        }
	        else
	        {
	        	sum="0";
	            editText.setText("0");
	        }
	    }
	    
	    public void onClickListener1(View v)
	    {
	        vibrator.vibrate(30);
	        
	        if(press=='=')
	        {
	            onClickListenerReset(buttonReset);
	        }
	       
	        one=(String)button1.getText();
	        sum=sum+one;
	        
	        editText.setText(sum);
	    }
	    
	    public void onClickListener2(View v)
	    {
	        vibrator.vibrate(30);
	        
	        if(press=='=')
	        {
	            onClickListenerReset(buttonReset);
	        }
	        
	       
	        two=(String)button2.getText();
	        sum=sum+two;
	        
	        editText.setText(sum);
	    }
	    
	    public void onClickListener3(View v)
	    {
	        vibrator.vibrate(30);
	        if(press=='=')
	        {
	            onClickListenerReset(buttonReset);
	        }
	        three=(String)button3.getText();
	        sum=sum+three;
	        
	        editText.setText(sum);
	    }
	    
	    public void onClickListener4(View v)
	    {
	        vibrator.vibrate(30);
	        
	        if(press=='=')
	        {
	            onClickListenerReset(buttonReset);
	        }
	        
	        four=(String)button4.getText();
	        sum=sum+four;
	        
	        editText.setText(sum);
	    }
	    
	    public void onClickListener5(View v)
	    {
	        vibrator.vibrate(30);
	        if(press=='=')
	        {
	            onClickListenerReset(buttonReset);
	        }
	        
	        five=(String)button5.getText();
	        sum=sum+five;
	        
	        editText.setText(sum);
	    }
	    
	    public void onClickListener6(View v)
	    {
	        vibrator.vibrate(30);
	        if(press=='=')
	        {
	            onClickListenerReset(buttonReset);
	        }
	        
	        six=(String)button6.getText();
	        sum=sum+six;
	        
	        editText.setText(sum);
	    }
	    
	    public void onClickListener7(View v)
	    {
	        vibrator.vibrate(30);
	        if(press=='=')
	        {
	            onClickListenerReset(buttonReset);
	        }
	        
	        seven=(String)button7.getText();
	        sum=sum+seven;
	        
	        editText.setText(sum);
	    }
	    
	    public void onClickListener8(View v)
	    {
	        vibrator.vibrate(30);
	        if(press=='=')
	        {
	            onClickListenerReset(buttonReset);
	        }
	        
	        eight=(String)button8.getText();
	        sum=sum+eight;
	        
	        editText.setText(sum);
	    }
	    
	    public void onClickListener9(View v)
	    {
	        vibrator.vibrate(30);
	        if(press=='=')
	        {
	            onClickListenerReset(buttonReset);
	        }
	        
	        nine=(String)button9.getText();
	        sum=sum+nine;
	        
	        editText.setText(sum);
	    }
	    
	    public void onClickListenerReset(View v)
	    {
	        vibrator.vibrate(30);
	       
	        sum="";
//	        countOne=0;//result=0;
//	        
//	        result=0f;
//	        result_mul=1f;
//	        result_div=1f;
	        press=' ';
//	        c=0;
	        
//	        editText.setText(result.toString());
	    }
	    public void onClickListenerPoint(View v)
	    {
	        vibrator.vibrate(30);
	        
	       int error=0;
	        
	        if(sum !=null)
	        {
	            for(int i=0;i<sum.length();i++)
	            {
	                if(sum.charAt(i)=='.')
	                {
	                    error=1;
	                    break;
	                }
	            }

	        }
	        
	        if(error==0)
	        {
	            if(sum==null)
	            {
	                sum=sum+"0.";
	            }
	            else
	            {
	                sum=sum+".";
	            }
	        }
	       
	        editText.setText(sum);
	    }
	    
	    public void onClickListener_del(View v)
	     {
	         vibrator.vibrate(30);
	         if(sum !="")
	         {
	             StringBuilder stringBuilder=new StringBuilder(80);
	             
	             stringBuilder.append(sum);
	             
	             sum=stringBuilder.deleteCharAt(stringBuilder.length()-1).toString();
	             
	             editText.setText(sum);
	         }
	         
	     }
}